package com.amigo.online.message.controller;

import com.amigo.online.common.param.dto.PushMessageDto;
import com.amigo.online.message.service.NoticeService;
import com.amigo.online.message.service.TokenManagerService;
import com.amigo.online.common.core.response.Result;
import com.amigo.online.message.util.onesignal.OneSignalUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 通知
 */
@RestController
@RequestMapping("/notice")
@Slf4j
public class NoticeContyoller {
    @Autowired
    TokenManagerService tokenManagerService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    OneSignalUtil oneSignalUtil;
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oneSignalId", value = "oneSignalId"),
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "token", paramType="header"),
    })
    @PostMapping(value = "/updateUserOneSignalId")
    public Result updateUserNoticeId(@RequestBody Map<String, String> map, @RequestHeader("ACCESS_TOKEN") String token){
        String oneSignalId = map.get("oneSignalId");
        if(oneSignalId==null){
            return Result.error("oneSignalId 不能为空");
        }
        boolean boo = tokenManagerService.checkToken(token);
        if (!boo){
            return Result.error("token校验失败");
        }
        return noticeService.updateUserNoticeId(oneSignalId, token);
    }

    /**
     * 发送通知
     * @return
     */
    @PostMapping(value = "/pushByOneSignalId")
    public Result pushByOneSignalId(@RequestBody PushMessageDto pushMessageDto) {
        log.info("给" + pushMessageDto.getUserId()[0] + "发送通知: " + pushMessageDto.getContents().get("zh-Hans"));
        oneSignalUtil.sendByUserId(pushMessageDto.getHeading(),pushMessageDto.getContents(), pushMessageDto.getUserId());
        return Result.success();
    }

}
